﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoragePeriod.DBUtility;
using System.Data;
using StoragePeriod.IDAL.StorageForecast;
using System.Data.SqlClient;
using StoragePeriod.Model.StorageForecast;

namespace StoragePeriod.SqlServerDAL.StorageForecast
{
    /// <summary>
    /// 数据访问类:ImgView
    /// </summary>
    public partial class D_ImgView : I_ImgView
    {
        public D_ImgView()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ImgView");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(E_ImgView model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ImgView(");
            strSql.Append("RawID,ImgTypeID,Imgname,ImgPath,UpLoadTime,UpLoadUserID,UpLoadUserName,Description)");
            strSql.Append(" values (");
            strSql.Append("@RawID,@ImgTypeID,@Imgname,@ImgPath,@UpLoadTime,@UpLoadUserID,@UpLoadUserName,@Description)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@RawID", SqlDbType.Int,4),
					new SqlParameter("@ImgTypeID", SqlDbType.Int,4),
					new SqlParameter("@Imgname", SqlDbType.VarChar,50),
					new SqlParameter("@ImgPath", SqlDbType.VarChar,200),
					new SqlParameter("@UpLoadTime", SqlDbType.DateTime),
					new SqlParameter("@UpLoadUserID", SqlDbType.Int,4),
					new SqlParameter("@UpLoadUserName", SqlDbType.VarChar,50),
					new SqlParameter("@Description", SqlDbType.Text)};
            parameters[0].Value = model.RawID;
            parameters[1].Value = model.ImgTypeID;
            parameters[2].Value = model.Imgname;
            parameters[3].Value = model.ImgPath;
            parameters[4].Value = model.UpLoadTime;
            parameters[5].Value = model.UpLoadUserID;
            parameters[6].Value = model.UpLoadUserName;
            parameters[7].Value = model.Description;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(E_ImgView model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ImgView set ");
            strSql.Append("RawID=@RawID,");
            strSql.Append("ImgTypeID=@ImgTypeID,");
            strSql.Append("Imgname=@Imgname,");
            strSql.Append("ImgPath=@ImgPath,");
            strSql.Append("UpLoadTime=@UpLoadTime,");
            strSql.Append("UpLoadUserID=@UpLoadUserID,");
            strSql.Append("UpLoadUserName=@UpLoadUserName,");
            strSql.Append("Description=@Description");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@RawID", SqlDbType.Int,4),
					new SqlParameter("@ImgTypeID", SqlDbType.Int,4),
					new SqlParameter("@Imgname", SqlDbType.VarChar,50),
					new SqlParameter("@ImgPath", SqlDbType.VarChar,200),
					new SqlParameter("@UpLoadTime", SqlDbType.DateTime),
					new SqlParameter("@UpLoadUserID", SqlDbType.Int,4),
					new SqlParameter("@UpLoadUserName", SqlDbType.VarChar,50),
					new SqlParameter("@Description", SqlDbType.Text),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.RawID;
            parameters[1].Value = model.ImgTypeID;
            parameters[2].Value = model.Imgname;
            parameters[3].Value = model.ImgPath;
            parameters[4].Value = model.UpLoadTime;
            parameters[5].Value = model.UpLoadUserID;
            parameters[6].Value = model.UpLoadUserName;
            parameters[7].Value = model.Description;
            parameters[8].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ImgView ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ImgView ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public E_ImgView GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,RawID,ImgTypeID,Imgname,ImgPath,UpLoadTime,UpLoadUserID,UpLoadUserName,Description from ImgView ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            E_ImgView model = new E_ImgView();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RawID"].ToString() != "")
                {
                    model.RawID = int.Parse(ds.Tables[0].Rows[0]["RawID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ImgTypeID"].ToString() != "")
                {
                    model.ImgTypeID = int.Parse(ds.Tables[0].Rows[0]["ImgTypeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Imgname"] != null)
                {
                    model.Imgname = ds.Tables[0].Rows[0]["Imgname"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ImgPath"] != null)
                {
                    model.ImgPath = ds.Tables[0].Rows[0]["ImgPath"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UpLoadTime"].ToString() != "")
                {
                    model.UpLoadTime = DateTime.Parse(ds.Tables[0].Rows[0]["UpLoadTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UpLoadUserID"].ToString() != "")
                {
                    model.UpLoadUserID = int.Parse(ds.Tables[0].Rows[0]["UpLoadUserID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UpLoadUserName"] != null)
                {
                    model.UpLoadUserName = ds.Tables[0].Rows[0]["UpLoadUserName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Description"] != null)
                {
                    model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,RawID,ImgTypeID,Imgname,ImgPath,UpLoadTime,UpLoadUserID,UpLoadUserName,Description ");
            strSql.Append(" FROM ImgView ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,RawID,ImgTypeID,Imgname,ImgPath,UpLoadTime,UpLoadUserID,UpLoadUserName,Description ");
            strSql.Append(" FROM ImgView ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  Method
    }
}
